<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\SysHomeBanner;

/**
 * SysHomeBannerSearch represents the model behind the search form about `backend\models\SysHomeBanner`.
 */
class SysHomeBannerSearch extends SysHomeBanner
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'type', 'designer_id', 'img_small_id', 'img_thumb_id', 'img_url_id', 'create_time', 'useing', 'order', 'action_id'], 'integer'],
            [['name', 'description', 'url', 'action'], 'safe'],
            [['top_time'], 'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SysHomeBanner::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'type' => $this->type,
            'designer_id' => $this->designer_id,
            'img_small_id' => $this->img_small_id,
            'img_thumb_id' => $this->img_thumb_id,
            'img_url_id' => $this->img_url_id,
            'create_time' => $this->create_time,
            'useing' => $this->useing,
            'order' => $this->order,
            'action_id' => $this->action_id,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'url', $this->url])
            ->andFilterWhere(['like', 'action', $this->action])
            ->andFilterWhere(['like', 'FROM_UNIXTIME(top_time, "%Y-%m-%d")', $this->setToptime($this->top_time)])
        ;

        return $dataProvider;
    }
    
    public function setToptime($string)
    {
    	if($string=='不置顶'){return 0;}
    	else {
    
    		return $string;
    	}
    }
}
